package cn.how2go.api.controller;


import cn.how2go.api.common.CommonResult;
import cn.how2go.api.entity.Device;
import cn.how2go.api.entity.Position;
import cn.how2go.api.service.PositionService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2020-05-31
 */
@Api(value = "位置",description = "位置信息的管理")
@RestController
@RequestMapping("/api/position")
public class PositionController {
    @Autowired
    private PositionService positionService;

    @ApiOperation(value="查询设备某段时间内的历史轨迹数据", notes="")
    @RequestMapping(value = "listPositionByDevice", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "ID", value = "设备ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "startTime",value = "开始时间，数据类型：yyyy-MM-dd HH:mm:ss", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "结束时间，数据类型：yyyy-MM-dd HH:mm:ss", required = false, dataType = "String"),
    })
    public CommonResult listPositionByDevice(
            @RequestParam(value = "ID", required = true) String id,
            @RequestParam(value = "startTime", required = true) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime){
        EntityWrapper<Position> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("Device",id);
        entityWrapper.ge("RecordTime",startTime);
        if(endTime!=null)
            entityWrapper.le("RecordTime",endTime);
        entityWrapper.orderBy("RecordTime",true);
        List<Position> list = positionService.selectList(entityWrapper);
        return CommonResult.success(list);
    }
}

